package com.example.travelservlet.util;

/**
 * @author hxy
 * @date 2022/12/23 15:57
 */
public class MyTime{

    //私有化时分秒
    private int hour;
    private int minute;
    private int second;

    public int getHour() {
        return hour;
    }

    public void setHour(int hour) {
        this.hour = hour;
    }

    public int getMinute() {
        return minute;
    }

    public void setMinute(int minute) {
        this.minute = minute;
    }

    public int getSecond() {
        return second;
    }

    public void setSecond(int second) {
        this.second = second;
    }

    public MyTime() {
    }

    //构造方法添加时分秒的时候进行判断，使得符合现实世界的逻辑
    public MyTime(int hour, int minute,int second) {
        if ( hour >= 0 && hour <= 24
                && minute >= 0 && minute <=60
                && second >= 0 && second <=60){
            this.hour = hour;
            this.minute = minute;
            this.second = second;
        }else{
            System.out.println("输入不合法");
        }

    }
    public String display(){
        return getHour() + ":" + getMinute() + ":" + getSecond() ;

    }


    //重写toString方法，成为符合国人审美的写法
    @Override
    public String toString() {
        return "北京时间" + " "+ hour +":" + minute ;
    }

    //获得当前时间的所有秒数之和
    public int AllSecond(){
        int AllSeconds = getHour() * 60 * 60 + getMinute() * 60 ;
        return AllSeconds;
    }

    //将秒数转换为时间
    public void ExpressTime(int AllSeconds){
        setMinute((AllSeconds % 3600) / 60);
        setHour(((AllSeconds) / 3600) % 24);
        display();
    }
    //增加秒的方法
    public void addSecond(int sec){
        System.out.print("增加" + sec + "秒后的时间为\t");
        ExpressTime(AllSecond() + sec);
    }
    //增加分的方法
    public void addMinute(int min){
        System.out.print("增加" + min + "分后的时间为\t");

        ExpressTime(AllSecond() + min * 60);
    }
    //增加小时的方法
    public void addHour(int hour){
        System.out.print("增加" + hour + "小时后的时间为\t");

        ExpressTime(AllSecond() + hour * 60 * 60);
    }
    //减少时间秒数的方法
    public void subSecond(int sec){
        int seconds = AllSecond();
        if(sec < seconds){
            System.out.print("减少" + sec + "秒后的时间为\t");
            ExpressTime(seconds - sec);
        }  else{
            subSecond(sec = sec - 60 * 60 *24);
        }
    }
    //减少时间分数的方法
    public void subMinute(int min){
        int seconds = AllSecond();
        if(min * 60 < seconds){
            System.out.print("减少" + min + "分后的时间为\t");
            ExpressTime(seconds - min * 60);
        }  else{
            subSecond(min = min * 60 - 60 * 60 *24);
        }
    }
    //减少时间小时数的方法
    public void subHour(int hour){
        int seconds = AllSecond();
        if(hour  * 60 * 60 < seconds){
            System.out.print("减少" + hour + "小时后的时间为\t");
            ExpressTime(seconds - hour * 60 * 60);
        }  else{
            subHour(hour = hour * 60 * 60 - 60 * 60 *24);
        }
    }

}


